package com.wd.dao.impl;

import cn.hutool.core.date.DateTime;
import com.wd.dao.LogDao;
import com.wd.entity.Blog;
import com.wd.entity.Log;
import com.wd.util.DBUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/11/9 17:35
 * @Description: ${description}
 * @Version: 1.0
 */
public class LogDaoImpl implements LogDao {
    @Override
    public List<Log> selLog() throws Exception {
        List<Log> list=new ArrayList<>();
        Log log=null;
        String sql="select * from log ORDER BY loginDate desc";
        ResultSet resultSet = DBUtil.executeQuery(sql);
        while (resultSet.next()){
            log=new Log();
            log.setId(Integer.parseInt(resultSet.getString("id")));
            log.setUsername(resultSet.getString("username"));
            log.setLoginDate(DateTime.of(resultSet.getString("loginDate"),"yyyy-MM-dd HH:mm:ss"));
            list.add(log);
        }
        DBUtil.closeAll();
        resultSet.close();
        return list;
    }

    @Override
    public void addLog(Log log) {
        String sql="insert into log values(default,?,?);";
        Object[] object=new Object[]{log.getUsername(),log.getLoginDate()};
        DBUtil.executeUpdate(sql,object);
        DBUtil.closeAll();
    }
}
